package com.example.mynetwork2.login;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.mynetwork2.MainActivity;
import com.example.mynetwork2.R;
import com.example.mynetwork2.login.utils.Utils;
import com.example.mynetwork2.mvpdesign.view.FanyiAct;
import com.example.mynetwork2.recyclerview.netrequest.ResponseBean;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.internal.Util;

public class LoginAct extends AppCompatActivity implements View.OnClickListener {
    private EditText input_username;
    private EditText input_password;
    private Button login_bt;
    private TextView register_tv;
    private OkHttpClient client ;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        getSupportActionBar().hide();
        input_username = (EditText) findViewById(R.id.et_loginactivity_username);
        input_password = (EditText) findViewById(R.id.et_loginactivity_password);
        login_bt = findViewById(R.id.bt_loginactivity_login);
        register_tv=findViewById(R.id.tv_loginactivity_register);
        //登录
        login_bt.setOnClickListener(this);
        //注册
        register_tv.setOnClickListener(this);
        client = new OkHttpClient();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_loginactivity_login:
                login();
                break;
            case  R.id.tv_loginactivity_register:
                register();
                break;
        }

    }


    private void login() {
        String username = input_username.getText().toString();
        String password = input_password.getText().toString();
        UserBean userBean = new UserBean();

        if (Utils.isEmpty(username)){
            Toast.makeText(this,"用户名不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if (Utils.isEmpty(password)){
            Toast.makeText(this,"密码不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if (Utils.checkEmail(username)){
            userBean.setEmail(username);
        }else if (Utils.isMobile(username)){
            userBean.setPhone(username);
        }else {
            userBean.setUsername(username);
        }
        userBean.setPassword(password);
        Gson gson = new Gson();
        String userInfoJson = gson.toJson(userBean);
        String loginUrl ="http://10.111.34.9:8080/user/login";
        //构造 RequestBody 需要指定MediaType，用于描述请求/响应 body 的内容类型
         MediaType typeToJson = MediaType.parse("application/json; charset=utf-8");

        Request request = new Request.Builder()
                .url(loginUrl)
                .post(RequestBody.create(userInfoJson,typeToJson ))
                .build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                Log.d("LoginFailure","登录失败: "+e.getMessage());
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String s = response.body().string();
                Gson gson1 = new Gson();
                Map<String,Object> map = gson1.fromJson(s, Map.class);
                Double code = (Double) map.get("code");

                if (code!=null&&code==0){
                    Intent intent = new Intent(LoginAct.this, MainActivity.class);
                    LoginAct.this.startActivity(intent);
                    LoginAct.this.finish();
                }else {
                    LoginAct.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(LoginAct.this, (String) map.get("msg"),Toast.LENGTH_SHORT);
                        }
                    });
                }



            }
        });


    }


    private void register() {
        Intent intent = new Intent(this, FanyiAct.class);
        startActivity(intent);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

    }
}
